package com.ruoyi.develop.lvRukumingxi.service.impl;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Pattern;

import com.ruoyi.common.core.domain.entity.SysArea;
import com.ruoyi.common.utils.DictUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.common.utils.uuid.IdUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.constants.BusinessConstants;
import com.ruoyi.develop.factory.factoryProduceMain.domain.FactoryProduceMain;
import com.ruoyi.develop.factory.factoryProduceMain.mapper.FactoryProduceMainMapper;
import com.ruoyi.develop.factory.factoryProduceSaleAll.domain.FactoryProduceSaleAll;
import com.ruoyi.develop.factory.factoryProduceSaleAll.mapper.FactoryProduceSaleAllMapper;
import com.ruoyi.develop.goodsOrderIn.domain.GoodsOrderIn;
import com.ruoyi.develop.goodsOrderIn.mapper.GoodsOrderInMapper;
import com.ruoyi.develop.goodsSale.domain.GoodsSale;
import com.ruoyi.develop.goodsSale.mapper.GoodsSaleMapper;
import com.ruoyi.develop.goodsSalebuy.domain.GoodsSalebuy;
import com.ruoyi.develop.goodsSalebuyall.domain.GoodsSalebuyall;
import com.ruoyi.develop.goodsSaledo.domain.GoodsSaledo;
import com.ruoyi.develop.goodsSaledoGongxuUserMoney.domain.GoodsSaledoGongxuUserMoney;
import com.ruoyi.develop.goodsSaledoGongxuUserMoney.service.IGoodsSaledoGongxuUserMoneyService;
import com.ruoyi.develop.goodsSaledoGongxuUserMoney.service.impl.GoodsSaledoGongxuUserMoneyServiceImpl;
import com.ruoyi.develop.goodsSupplier.domain.GoodsSupplier;
import com.ruoyi.develop.goodsSupplier.service.IGoodsSupplierService;
import com.ruoyi.develop.lvRuku.domain.LvRuku;
import com.ruoyi.develop.lvRuku.service.ILvRukuService;
import com.ruoyi.export.service.impl.ExportServiceImpl;
import org.apache.poi.ss.usermodel.DateUtil;
import org.jxls.reader.ReaderBuilder;
import org.jxls.reader.XLSReadStatus;
import org.jxls.reader.XLSReader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import com.ruoyi.develop.lvRukumingxi.mapper.LvRukumingxiMapper;
import com.ruoyi.develop.lvRukumingxi.domain.LvRukumingxi;
import com.ruoyi.develop.lvRukumingxi.service.ILvRukumingxiService;
import org.springframework.web.multipart.MultipartFile;

/**
 * 出入库记录Service业务层处理
 *
 * @author qh
 * @date 2024-10-12
 */
@Service
public class LvRukumingxiServiceImpl implements ILvRukumingxiService {
    @Resource
    private LvRukumingxiMapper lvRukumingxiMapper;

    @Resource
    private GoodsOrderInMapper goodsOrderInMapper;

    @Resource
    private FactoryProduceSaleAllMapper factoryProduceSaleAllMapper;
    @Resource
    private FactoryProduceMainMapper factoryProduceMainMapper;
    @Resource
    private GoodsSaleMapper goodsSaleMapper;
    @Autowired
    private ILvRukuService lvRukuService;
    @Autowired
    private IGoodsSaledoGongxuUserMoneyService goodsSaledoGongxuUserMoneyService;
    @Autowired
    private IGoodsSupplierService iGoodsSupplierService;

    @Value(value = "${ruoyi.path}")
    public String basePath;
    @Value(value = "${ruoyi.templatePath}")
    public String templatePath;
    @Value(value = "${ruoyi.templateImport}")
    public String templateImport;

    @Autowired
    private ExportServiceImpl exportService;
    /**
     * 查询出入库记录
     *
     * @param id 出入库记录主键
     * @return 出入库记录
     */
    @Override
    public LvRukumingxi selectById(String id) {
        return lvRukumingxiMapper.selectById(id);
    }

    /**
     * 查询出入库记录列表
     *
     * @param entity 出入库记录
     * @return 出入库记录
     */
    @Override
    public List<LvRukumingxi> selectList(LvRukumingxi entity) {
        return lvRukumingxiMapper.selectList(entity);
    }

    /**
     * 查询采购入库 可用数量大于0 的列表
     *
     * @param entity 出入库记录
     * @return 出入库记录
     */
    @Override
    public List<LvRukumingxi> selectListRemainNotZero(LvRukumingxi entity) {
        return lvRukumingxiMapper.selectListRemainNotZero(entity);
    }




    /**
     * 新增出入库记录
     *
     * @param entity 出入库记录
     * @return 结果
     */
    @Override
    public int insert(LvRukumingxi entity) {
        if (StringUtils.isBlank(entity.getId())) {
            entity.setId(IdUtils.fastSimpleUUID());
        }
        entity.setCreateBy(SecurityUtils.getUsername());
        entity.setCreateTime(DateUtils.getNowDate());
        return lvRukumingxiMapper.insert(entity);
    }

    /**
     * 修改出入库记录
     *
     * @param entity 出入库记录
     * @return 结果
     */
    @Override
    public int update(LvRukumingxi entity) {
        entity.setUpdateBy(SecurityUtils.getUsername());
        entity.setUpdateTime(DateUtils.getNowDate());
        return lvRukumingxiMapper.update(entity);
    }

    @Override
    public int editBohui(LvRukumingxi entity) {
        GoodsOrderIn goodsOrderIn = goodsOrderInMapper.selectById(entity.getGoodsId());
        goodsOrderIn.setCount(goodsOrderIn.getCount().subtract(entity.getDaohuoCount()));
        goodsOrderIn.setRemainderCount(goodsOrderIn.getRemainderCount().subtract(entity.getDaohuoCount()));
        goodsOrderInMapper.update(goodsOrderIn);
        entity.setUpdateBy(SecurityUtils.getUsername());
        entity.setUpdateTime(DateUtils.getNowDate());
        return lvRukumingxiMapper.update(entity);
    }

    @Override
    public int ruku(String[] ids) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String today = sdf.format(new Date());
        String codeMaxString = lvRukuService.selectMaxCode(today);
        if(StringUtils.isNotBlank(codeMaxString)){
            // 最大编号+1
            codeMaxString = Long.parseLong(codeMaxString)+1+"";
        }else{
            codeMaxString = today+"0001";
        }
        LvRuku lvRuku = new LvRuku();
        lvRuku.setNos(codeMaxString);
        lvRuku.setState(BusinessConstants.GOODS_8);
        lvRuku.setRukuDate(new Date());
        lvRuku.setRukuid(SecurityUtils.getLoginUser().getUserId());
        lvRuku.setRukupeople(SecurityUtils.getLoginUser().getUsername());
        lvRuku.setType(BusinessConstants.TYPE_RUKU);
        lvRuku.setSource(BusinessConstants.RUKU_SOURCE_PRODUCE);
        lvRuku.setYwType(BusinessConstants.RUKU_TYPE_8);
        lvRuku.setSupplierName("库存");
        lvRuku.setStatus(BusinessConstants.STORAGE_STATE_YES);
        /*lvRuku.setMainId(goodsSale.getProjectId());
        lvRuku.setName(projectBaseInfo.getName());
        lvRuku.setCode(projectBaseInfo.getCode());*/
        lvRukuService.insert(lvRuku);

        for(String id :ids){
            LvRukumingxi lvRukumingxi= lvRukumingxiMapper.selectById(id);
            GoodsOrderIn goodsOrderIn = goodsOrderInMapper.selectById(lvRukumingxi.getGoodsId());
            goodsOrderIn.setCount(goodsOrderIn.getCount().add(lvRukumingxi.getDaohuoCount()));
            goodsOrderIn.setRemainderCount(goodsOrderIn.getRemainderCount().add(lvRukumingxi.getDaohuoCount()));
            goodsOrderInMapper.update(goodsOrderIn);
            //lvRukumingxi.setStatus(BusinessConstants.STORAGE_STATE_YES);
            lvRukumingxi.setCanCount(lvRukumingxi.getDaohuoCount());
            lvRukumingxi.setStatus(BusinessConstants.STORAGE_STATE_YES);
            lvRukumingxi.setRukudate(new Date());
            lvRukumingxi.setRukuren(SecurityUtils.getUsername());
            lvRukumingxi.setSupplierName("库存");
            lvRukumingxi.setGoodsType(BusinessConstants.RUKU_TYPE_8);
            lvRukumingxi.setRukuno(codeMaxString);
            lvRukumingxi.setNos(codeMaxString);
            lvRukumingxi.setParentId(lvRuku.getId());

            /*GoodsSaledoGongxuUserMoney goodsSaledoGongxuUserMoney = goodsSaledoGongxuUserMoneyService.selectByGoodssaleId(lvRukumingxi.getFactoryGoodssaleId());
            LvRukumingxi lvRukumingxiSum = lvRukumingxiMapper.selectByGoodssaleId(lvRukumingxi.getFactoryGoodssaleId());
            BigDecimal lvRukumingxiSumTotilPrice=BigDecimal.ZERO;
            if(lvRukumingxiSum!=null){
                lvRukumingxiSumTotilPrice=lvRukumingxiSum.getWorkMoney();
            }
            if(goodsSaledoGongxuUserMoney!=null){
                if(goodsSaledoGongxuUserMoney.getWorkMoney()!=null   ){
                    LvRukumingxi lvRukumingxiBeginMoney=lvRukumingxiMapper.selectBeginMoney();
                    lvRukumingxi.setGoodsBuyTotal(goodsSaledoGongxuUserMoney.getWorkMoney().add(lvRukumingxiSumTotilPrice)
                            .add(goodsOrderIn.getShuidianFee().add(goodsOrderIn.getShebeiFee()).add(goodsOrderIn.getMojuFee())
                                    .add(goodsOrderIn.getFuliaoFee()).add(goodsOrderIn.getWeibaoFee()).add(goodsOrderIn.getFeiliaoFee())
                                    .add(goodsOrderIn.getYewuFee()).add(goodsOrderIn.getUserFee())));
                    if(lvRukumingxi.getDaohuoCount()!=null){
                        lvRukumingxi.setGoodsBuyPrice(goodsSaledoGongxuUserMoney.getWorkMoney().divide(lvRukumingxi.getDaohuoCount()));
                    }
                    lvRukumingxi.setBeginMoney(lvRukumingxiBeginMoney.getBeginMoney().add(goodsSaledoGongxuUserMoney.getWorkMoney()));
                }
            }*/
            update(lvRukumingxi);

            FactoryProduceSaleAll factoryProduceSaleAll=factoryProduceSaleAllMapper.selectById(lvRukumingxi.getFactoryGoodssaleId());
            if(factoryProduceSaleAll!=null){
                factoryProduceSaleAll.setStatus(BusinessConstants.PROD_STATUS_6);//已入库
                //factoryProduceSaleAll.setDaizhijianCount(factoryProduceSaleAll.getDaizhijianCount().subtract(lvRukumingxi.getDaohuoCount()));
                factoryProduceSaleAll.setRukuCount(factoryProduceSaleAll.getRukuCount().add(lvRukumingxi.getDaohuoCount()));
                factoryProduceSaleAll.setWeiruCount(factoryProduceSaleAll.getCount().subtract(lvRukumingxi.getDaohuoCount()));
                factoryProduceSaleAllMapper.update(factoryProduceSaleAll);
                List<FactoryProduceSaleAll> factoryProduceSaleAlls=factoryProduceSaleAllMapper.selectHaveDo(factoryProduceSaleAll.getProduceId());
                if(factoryProduceSaleAlls.size()>0){

                }else{
                    FactoryProduceMain factoryProduceMain=factoryProduceMainMapper.selectById(factoryProduceSaleAll.getProduceId());
                    factoryProduceMain.setStatus(BusinessConstants.PROD_STATUS_6);
                    factoryProduceMain.setIpoStatus(BusinessConstants.PROD_STATUS_6);
                    factoryProduceMainMapper.update(factoryProduceMain);
                }
            }
            //goodsSaleMapper.updateStatusById(lvRukumingxi.getGoodsSaleId(),BusinessConstants.SALE_TYPE_9);
        }

        return ids.length;
    }
   //获取期初余额
   public LvRukumingxi zhijianSuccess() {
       return lvRukumingxiMapper.selectBeginMoney();
   }

    @Override
    public int zhijianSuccess(String[] ids) {
        for(String id :ids){
            LvRukumingxi lvRukumingxi= lvRukumingxiMapper.selectById(id);
            lvRukumingxi.setIsZhijian(BusinessConstants.INSPECT_STATE_YES);
            lvRukumingxi.setStatus(BusinessConstants.STORAGE_STATE_UN);
            lvRukumingxi.setZhijianDate(new Date());
            lvRukumingxi.setZhijianName(SecurityUtils.getUsername());
            goodsSaleMapper.updateStatusById(lvRukumingxi.getGoodsSaleId(),BusinessConstants.SALE_TYPE_8);
            lvRukumingxiMapper.update(lvRukumingxi);

           /* GoodsSale goodsSale1=goodsSaleMapper.selectById(lvRukumingxi.getGoodsSaleId());
            if(goodsSale1!=null){
                goodsSale1.setStatus(BusinessConstants.GOODS_8);//待入库
                goodsSaleMapper.update(goodsSale1);
            }*/
        }
        return ids.length;
    }

    @Override
    public int zizhiToZhijian(String[] ids) {
        for(String id :ids){
            LvRukumingxi lvRukumingxi= lvRukumingxiMapper.selectById(id);
            lvRukumingxi.setIsZhijian(BusinessConstants.INSPECT_STATE_UN);
            lvRukumingxi.setStatus(BusinessConstants.STORAGE_STATE_UN);
            lvRukumingxi.setZhijianDate(null);
            lvRukumingxi.setZhijianName(null);
            lvRukumingxiMapper.update(lvRukumingxi);

            FactoryProduceSaleAll factoryProduceSaleAll=factoryProduceSaleAllMapper.selectById(lvRukumingxi.getFactoryGoodssaleId());
            if(factoryProduceSaleAll!=null){
                factoryProduceSaleAll.setStatus(BusinessConstants.PROD_STATUS_8);//已质检
                factoryProduceSaleAll.setUpdateBy(SecurityUtils.getUsername());
                factoryProduceSaleAll.setUpdateTime(DateUtils.getNowDate());
                factoryProduceSaleAllMapper.update(factoryProduceSaleAll);
            }
           /* GoodsSale goodsSale1=goodsSaleMapper.selectById(lvRukumingxi.getGoodsSaleId());
            if(goodsSale1!=null){
                goodsSale1.setStatus(BusinessConstants.GOODS_8);//待入库
                goodsSaleMapper.update(goodsSale1);
            }*/
        }
        return ids.length;
    }

    /**
     * 批量删除出入库记录
     *
     * @param ids 需要删除的出入库记录主键
     * @return 结果
     */
    @Override
    public int deleteByIds(String[] ids) {
        return lvRukumingxiMapper.deleteByIds(ids);
    }

    /**
     * 删除出入库记录信息
     *
     * @param id 出入库记录主键
     * @return 结果
     */
    @Override
    public int deleteById(String id) {
        return lvRukumingxiMapper.deleteById(id);
    }


    @Override
    public int deleteByIdPhysical(String id) {
        return lvRukumingxiMapper.deleteByIdPhysical(id);
    }

    @Override
    public int insertIn(LvRukumingxi entity) {
        GoodsOrderIn goodsOrderIn = goodsOrderInMapper.selectById(entity.getGoodsId());
        goodsOrderIn.setCount(goodsOrderIn.getCount().add(entity.getDaohuoCount()));
        goodsOrderIn.setRemainderCount(goodsOrderIn.getRemainderCount().add(entity.getDaohuoCount()));
        goodsOrderInMapper.update(goodsOrderIn);
        entity.setCanCount(entity.getDaohuoCount());
        entity.setStatus(BusinessConstants.STORAGE_STATE_YES);
        return this.insert(entity);
    }

    @Override
    public int addInZizhi(LvRukumingxi entity) {

        entity.setCanCount(entity.getDaohuoCount());
        entity.setStatus(BusinessConstants.INSPECT_STATE_UN);
        entity.setIsdo(BusinessConstants.GOODS_DO_YES);
        return this.insert(entity);
    }

    @Override
    public int insertOut(LvRukumingxi entity) {
        GoodsOrderIn goodsOrderIn = goodsOrderInMapper.selectById(entity.getGoodsId());
        goodsOrderIn.setCount(goodsOrderIn.getCount().subtract(entity.getDaohuoCount()));
        goodsOrderIn.setRemainderCount(goodsOrderIn.getRemainderCount().subtract(entity.getDaohuoCount()));
        goodsOrderInMapper.update(goodsOrderIn);
        entity.setStatus(BusinessConstants.STORAGE_STATE_YES);
        return this.insert(entity);
    }

    @Override
    public Map<String, Object> uploadChanchengpin(LvRukumingxi entity, MultipartFile file) {
        String tmplName = "uploadChanchengpinMingxi.xml";
        String errortext = "";
        String codes = "";
        Map<String, Object> map = new HashMap<>();
        int i = 0;
        int j = 0;
        List<LvRukumingxi> list = new ArrayList<>();
        try {
            InputStream in = new FileInputStream(basePath + File.separator + templatePath + File.separator + templateImport + File.separator + tmplName);
            XLSReader mainReader = ReaderBuilder.buildFromXML(in);
            Map beans = new HashMap();
            List<LvRukumingxi> listMingxi = new ArrayList<>();
            beans.put("list", list);
            XLSReadStatus readStatus = mainReader.read(file.getInputStream(), beans);
            if (readStatus.isStatusOK()) {
                for (LvRukumingxi lvRukumingxi : list) {
                    LvRukumingxi lvRukumingxiNew=new LvRukumingxi();
                    if (StringUtils.isNotBlank(lvRukumingxi.getCode())) {
                        GoodsOrderIn goodsOrderIn=goodsOrderInMapper.selectByCode(lvRukumingxi.getCode());
                        if(goodsOrderIn!=null){
                            //lvRukumingxi1.setCode(lvRukumingxi.getCode());
                            BeanUtils.copyProperties(lvRukumingxi, lvRukumingxiNew, "id");
                            lvRukumingxiNew.setGoodsId(goodsOrderIn.getId());
                            lvRukumingxiNew.setStatus(BusinessConstants.STORAGE_STATE_YES);
                            lvRukumingxiNew.setIsZhijian(BusinessConstants.INSPECT_STATE_YES);
                            lvRukumingxiNew.setType(BusinessConstants.TYPE_RUKU);
                            lvRukumingxiNew.setGoodsType(BusinessConstants.GOODS_TYPE_HARDWARE);
                            lvRukumingxiNew.setIsdo(BusinessConstants.GOODS_DO_YES);
                            try {
                                boolean arriveTimeMatch = Pattern.matches("[0-9]+[.]{0,1}[0-9]*[dD]{0,1}", lvRukumingxi.getRukudateStr());
                                if (!arriveTimeMatch) {
                                    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
                                    lvRukumingxiNew.setRukudate(df.parse(lvRukumingxi.getRukudateStr()));
                                } else {
                                    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
                                    String date = df.format(DateUtil.getJavaDate(Double.parseDouble(lvRukumingxi.getRukudateStr())));
                                    lvRukumingxiNew.setRukudate(DateUtils.parseDate(date, "yyyy-MM-dd"));
                                }
                            } catch (Exception e) {
                                errortext += "行号为 " + lvRukumingxi.getIdx() + "行的数据，入库时间时间格式错误，请修改后再上传！";
//                        map.put("errorText", errortext);
//                        return map;
                                break;
                            }
                            try {
                                if(StringUtils.isNotBlank(lvRukumingxi.getDaohuoCountStr())){
                                    lvRukumingxiNew.setDaohuoCount(new BigDecimal(lvRukumingxi.getDaohuoCountStr()));
                                    lvRukumingxiNew.setCanCount(new BigDecimal(lvRukumingxi.getDaohuoCountStr()));
                                }
                            } catch (Exception e) {
                                errortext += "行号为 " + lvRukumingxi.getIdx() + "行的数据，数量格式错误，请修改后再上传！";
                                break;
                            }
                            try {
                                if(StringUtils.isNotBlank(lvRukumingxi.getGoodsBuyPriceStr())){
                                    lvRukumingxiNew.setGoodsBuyPrice(new BigDecimal(lvRukumingxi.getGoodsBuyPriceStr()));
                                }
                            } catch (Exception e) {
                                errortext += "行号为 " + lvRukumingxi.getIdx() + "行的数据，单价格式错误，请修改后再上传！";
                                break;
                            }
                            try {
                                if(StringUtils.isNotBlank(lvRukumingxi.getGoodsTotalPriceStr())){
                                    lvRukumingxiNew.setGoodsTotalPrice(new BigDecimal(lvRukumingxi.getGoodsTotalPriceStr()));
                                }
                            } catch (Exception e) {
                                errortext += "行号为 " + lvRukumingxi.getIdx() + "行的数据，金额格式错误，请修改后再上传！";
                                break;
                            }
                            listMingxi.add(lvRukumingxiNew);
                        } else{
                            goodsOrderIn=goodsOrderInMapper.selectByOtherCode(lvRukumingxi.getCode()+",");
                            if(goodsOrderIn!=null){
                                BeanUtils.copyProperties(lvRukumingxi, lvRukumingxiNew, "id");
                                lvRukumingxiNew.setGoodsId(goodsOrderIn.getId());
                                lvRukumingxiNew.setStatus(BusinessConstants.STORAGE_STATE_YES);
                                lvRukumingxiNew.setIsZhijian(BusinessConstants.INSPECT_STATE_YES);
                                lvRukumingxiNew.setType(BusinessConstants.TYPE_RUKU);
                                lvRukumingxiNew.setGoodsType(BusinessConstants.GOODS_TYPE_HARDWARE);
                                lvRukumingxiNew.setIsdo(BusinessConstants.GOODS_DO_YES);
                                try {
                                    boolean arriveTimeMatch = Pattern.matches("[0-9]+[.]{0,1}[0-9]*[dD]{0,1}", lvRukumingxi.getRukudateStr());
                                    if (!arriveTimeMatch) {
                                        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
                                        lvRukumingxiNew.setRukudate(df.parse(lvRukumingxi.getRukudateStr()));
                                    } else {
                                        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
                                        String date = df.format(DateUtil.getJavaDate(Double.parseDouble(lvRukumingxi.getRukudateStr())));
                                        lvRukumingxiNew.setRukudate(DateUtils.parseDate(date, "yyyy-MM-dd"));
                                    }
                                } catch (Exception e) {
                                    errortext += "行号为 " + lvRukumingxi.getIdx() + "行的数据，入库时间时间格式错误，请修改后再上传！";
                                    break;
                                }
                                try {
                                    if(StringUtils.isNotBlank(lvRukumingxi.getDaohuoCountStr())){
                                        lvRukumingxiNew.setDaohuoCount(new BigDecimal(lvRukumingxi.getDaohuoCountStr()));
                                        lvRukumingxiNew.setCanCount(new BigDecimal(lvRukumingxi.getDaohuoCountStr()));
                                    }
                                } catch (Exception e) {
                                    errortext += "行号为 " + lvRukumingxi.getIdx() + "行的数据，数量格式错误，请修改后再上传！";
                                    break;
                                }
                                try {
                                    if(StringUtils.isNotBlank(lvRukumingxi.getGoodsBuyPriceStr())){
                                        lvRukumingxiNew.setGoodsBuyPrice(new BigDecimal(lvRukumingxi.getGoodsBuyPriceStr()));
                                    }
                                } catch (Exception e) {
                                    errortext += "行号为 " + lvRukumingxi.getIdx() + "行的数据，单价格式错误，请修改后再上传！";
                                    break;
                                }
                                try {
                                    if(StringUtils.isNotBlank(lvRukumingxi.getGoodsTotalPriceStr())){
                                        lvRukumingxiNew.setGoodsTotalPrice(new BigDecimal(lvRukumingxi.getGoodsTotalPriceStr()));
                                    }
                                } catch (Exception e) {
                                    errortext += "行号为 " + lvRukumingxi.getIdx() + "行的数据，金额格式错误，请修改后再上传！";
                                    break;
                                }
                                listMingxi.add(lvRukumingxiNew);
                            }
                            else {
                                codes = codes+lvRukumingxi.getCode()+",";
                               /* errortext += "序号为" + lvRukumingxi.getIdx() + "行数据错误，存货编号："+lvRukumingxi.getCode()+"在成品库中不存在，请在产品库中新建新产品后导入！";
                                map.put("errorText", errortext);
                                map.put("num", i);
                                map.put("numupdate", j);
                                return map;*/
                            }
                        }
                    }
                   // i++;
                    if (StringUtils.isNotBlank(errortext)) {
                        map.put("errorText", errortext);
                        map.put("num", i);
                        map.put("numupdate", j);
                        return map;
                    }
                }
            }
            if (StringUtils.isBlank(errortext)) {
                for (LvRukumingxi info : listMingxi) {
                    i += this.insert(info);
                    GoodsOrderIn goodsOrderInUodate=goodsOrderInMapper.selectById(info.getGoodsId());
                    goodsOrderInUodate.setCount(goodsOrderInUodate.getCount().add(info.getDaohuoCount()));
                    goodsOrderInUodate.setRemainderCount(goodsOrderInUodate.getRemainderCount().add(info.getDaohuoCount()));
                    goodsOrderInUodate.setCostPrice(info.getGoodsBuyPrice());
                    goodsOrderInUodate.setCost((info.getGoodsBuyPrice().multiply(goodsOrderInUodate.getRemainderCount())));
                    goodsOrderInMapper.update(goodsOrderInUodate);

                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        map.put("errorText", errortext);
        map.put("num", i);
        map.put("codes", codes);
        map.put("numupdate", j);
        return map;
    }

    @Override
    public Map<String, Object> uploadCaigou(LvRukumingxi entity, MultipartFile file) {
        String tmplName = "uploadCaigouMingxi.xml";
        String errortext = "";
        String codes = "";
        Map<String, Object> map = new HashMap<>();
        int i = 0;
        int j = 0;
        List<LvRukumingxi> list = new ArrayList<>();
        try {
            InputStream in = new FileInputStream(basePath + File.separator + templatePath + File.separator + templateImport + File.separator + tmplName);
            XLSReader mainReader = ReaderBuilder.buildFromXML(in);
            Map beans = new HashMap();
            List<LvRukumingxi> listMingxi = new ArrayList<>();
            beans.put("list", list);
            XLSReadStatus readStatus = mainReader.read(file.getInputStream(), beans);
            if (readStatus.isStatusOK()) {
                for (LvRukumingxi lvRukumingxi : list) {
                    LvRukumingxi lvRukumingxiNew=new LvRukumingxi();
                    if (StringUtils.isNotBlank(lvRukumingxi.getCode())) {
                        GoodsOrderIn goodsOrderIn=goodsOrderInMapper.selectByCode(lvRukumingxi.getCode());
                        if(goodsOrderIn!=null){
                            //lvRukumingxi1.setCode(lvRukumingxi.getCode());
                            BeanUtils.copyProperties(lvRukumingxi, lvRukumingxiNew, "id");
                            lvRukumingxiNew.setGoodsId(goodsOrderIn.getId());
                            lvRukumingxiNew.setStatus(BusinessConstants.STORAGE_STATE_YES);
                            lvRukumingxiNew.setIsZhijian(BusinessConstants.INSPECT_STATE_YES);
                            lvRukumingxiNew.setType(BusinessConstants.TYPE_RUKU);
                            lvRukumingxiNew.setGoodsType(BusinessConstants.GOODS_TYPE_HARDWARE);
                            lvRukumingxiNew.setIsdo(BusinessConstants.GOODS_DO_NO);
                            try {
                                boolean arriveTimeMatch = Pattern.matches("[0-9]+[.]{0,1}[0-9]*[dD]{0,1}", lvRukumingxi.getRukudateStr());
                                if (!arriveTimeMatch) {
                                    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
                                    lvRukumingxiNew.setRukudate(df.parse(lvRukumingxi.getRukudateStr()));
                                } else {
                                    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
                                    String date = df.format(DateUtil.getJavaDate(Double.parseDouble(lvRukumingxi.getRukudateStr())));
                                    lvRukumingxiNew.setRukudate(DateUtils.parseDate(date, "yyyy-MM-dd"));
                                }
                            } catch (Exception e) {
                                errortext += "行号为 " + lvRukumingxi.getIdx() + "行的数据，入库时间时间格式错误，请修改后再上传！";
//                        map.put("errorText", errortext);
//                        return map;
                                break;
                            }
                            try {
                                if(StringUtils.isNotBlank(lvRukumingxi.getDaohuoCountStr())){
                                    lvRukumingxiNew.setDaohuoCount(new BigDecimal(lvRukumingxi.getDaohuoCountStr()));
                                    lvRukumingxiNew.setCanCount(new BigDecimal(lvRukumingxi.getDaohuoCountStr()));
                                }
                            } catch (Exception e) {
                                errortext += "行号为 " + lvRukumingxi.getIdx() + "行的数据，数量格式错误，请修改后再上传！";
                                break;
                            }
                            try {
                                if(StringUtils.isNotBlank(lvRukumingxi.getGoodsTotalPriceStr())){
                                    lvRukumingxiNew.setGoodsTotalPrice(new BigDecimal(lvRukumingxi.getGoodsTotalPriceStr()));
                                }
                            } catch (Exception e) {
                                errortext += "行号为 " + lvRukumingxi.getIdx() + "行的数据，金额格式错误，请修改后再上传！";
                                break;
                            }
                            listMingxi.add(lvRukumingxiNew);
                        }else{
                            goodsOrderIn=goodsOrderInMapper.selectByOtherCode(lvRukumingxi.getCode()+",");
                            if(goodsOrderIn!=null){
                                BeanUtils.copyProperties(lvRukumingxi, lvRukumingxiNew, "id");
                                lvRukumingxiNew.setGoodsId(goodsOrderIn.getId());
                                lvRukumingxiNew.setStatus(BusinessConstants.STORAGE_STATE_YES);
                                lvRukumingxiNew.setIsZhijian(BusinessConstants.INSPECT_STATE_YES);
                                lvRukumingxiNew.setType(BusinessConstants.TYPE_RUKU);
                                lvRukumingxiNew.setGoodsType(BusinessConstants.GOODS_TYPE_HARDWARE);
                                lvRukumingxiNew.setIsdo(BusinessConstants.GOODS_DO_NO);
                                try {
                                    boolean arriveTimeMatch = Pattern.matches("[0-9]+[.]{0,1}[0-9]*[dD]{0,1}", lvRukumingxi.getRukudateStr());
                                    if (!arriveTimeMatch) {
                                        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
                                        lvRukumingxiNew.setRukudate(df.parse(lvRukumingxi.getRukudateStr()));
                                    } else {
                                        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
                                        String date = df.format(DateUtil.getJavaDate(Double.parseDouble(lvRukumingxi.getRukudateStr())));
                                        lvRukumingxiNew.setRukudate(DateUtils.parseDate(date, "yyyy-MM-dd"));
                                    }
                                } catch (Exception e) {
                                    errortext += "行号为 " + lvRukumingxi.getIdx() + "行的数据，入库时间时间格式错误，请修改后再上传！";
//                        map.put("errorText", errortext);
//                        return map;
                                    break;
                                }
                                try {
                                    if(StringUtils.isNotBlank(lvRukumingxi.getDaohuoCountStr())){
                                        lvRukumingxiNew.setDaohuoCount(new BigDecimal(lvRukumingxi.getDaohuoCountStr()));
                                        lvRukumingxiNew.setCanCount(new BigDecimal(lvRukumingxi.getDaohuoCountStr()));
                                    }
                                } catch (Exception e) {
                                    errortext += "行号为 " + lvRukumingxi.getIdx() + "行的数据，数量格式错误，请修改后再上传！";
                                    break;
                                }
                                try {
                                    if(StringUtils.isNotBlank(lvRukumingxi.getGoodsTotalPriceStr())){
                                        lvRukumingxiNew.setGoodsTotalPrice(new BigDecimal(lvRukumingxi.getGoodsTotalPriceStr()));
                                        lvRukumingxiNew.setGoodsBuyPrice((new BigDecimal(lvRukumingxi.getGoodsTotalPriceStr())).divide(new BigDecimal(lvRukumingxi.getDaohuoCountStr())));
                                    }
                                } catch (Exception e) {
                                    errortext += "行号为 " + lvRukumingxi.getIdx() + "行的数据，金额格式错误，请修改后再上传！";
                                    break;
                                }
                                listMingxi.add(lvRukumingxiNew);
                            }
                            else {
                                codes = codes+lvRukumingxi.getCode()+",";
                               /* errortext += "序号为" + lvRukumingxi.getIdx() + "行数据错误，存货编号："+lvRukumingxi.getCode()+"在成品库中不存在，请在产品库中新建新产品后导入！";
                                map.put("errorText", errortext);
                                map.put("num", i);
                                map.put("numupdate", j);
                                return map;*/
                            }
                        }
                    }
                    // i++;
                    if (StringUtils.isNotBlank(errortext)) {
                        map.put("errorText", errortext);
                        map.put("num", i);
                        map.put("numupdate", j);
                        return map;
                    }
                }
            }
            if (StringUtils.isBlank(errortext)) {
                for (LvRukumingxi info : listMingxi) {
                    i += this.insert(info);
                    GoodsOrderIn goodsOrderInUodate=goodsOrderInMapper.selectById(info.getGoodsId());
                    goodsOrderInUodate.setCount(goodsOrderInUodate.getCount().add(info.getDaohuoCount()));
                    goodsOrderInUodate.setRemainderCount(goodsOrderInUodate.getRemainderCount().add(info.getDaohuoCount()));
                    goodsOrderInUodate.setCostPrice(info.getGoodsBuyPrice());
                    if(info.getGoodsBuyPrice() !=null && goodsOrderInUodate.getRemainderCount()!=null){
                        goodsOrderInUodate.setCost((info.getGoodsBuyPrice().multiply(goodsOrderInUodate.getRemainderCount())));
                    }
                    goodsOrderInMapper.update(goodsOrderInUodate);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        map.put("errorText", errortext);
        map.put("num", i);
        map.put("codes", codes);
        map.put("numupdate", j);
        return map;
    }
    @Override
    public Map<String, Object> uploadOther(LvRukumingxi entity, MultipartFile file) {
        String tmplName = "uploadOtherMingxi.xml";
        String errortext = "";
        String codes = "";
        Map<String, Object> map = new HashMap<>();
        int i = 0;
        int j = 0;
        List<LvRukumingxi> list = new ArrayList<>();
        try {
            InputStream in = new FileInputStream(basePath + File.separator + templatePath + File.separator + templateImport + File.separator + tmplName);
            XLSReader mainReader = ReaderBuilder.buildFromXML(in);
            Map beans = new HashMap();
            List<LvRukumingxi> listMingxi = new ArrayList<>();
            beans.put("list", list);
            XLSReadStatus readStatus = mainReader.read(file.getInputStream(), beans);
            if (readStatus.isStatusOK()) {
                for (LvRukumingxi lvRukumingxi : list) {
                    LvRukumingxi lvRukumingxiNew=new LvRukumingxi();
                    if (StringUtils.isNotBlank(lvRukumingxi.getCode())) {
                        GoodsOrderIn goodsOrderIn=goodsOrderInMapper.selectByCode(lvRukumingxi.getCode());
                        if(goodsOrderIn!=null){
                            //lvRukumingxi1.setCode(lvRukumingxi.getCode());
                            BeanUtils.copyProperties(lvRukumingxi, lvRukumingxiNew, "id");
                            lvRukumingxiNew.setGoodsId(goodsOrderIn.getId());
                            lvRukumingxiNew.setStatus(BusinessConstants.STORAGE_STATE_YES);
                            lvRukumingxiNew.setIsZhijian(BusinessConstants.INSPECT_STATE_YES);
                            lvRukumingxiNew.setType(BusinessConstants.TYPR_CHUKU);
                            lvRukumingxiNew.setGoodsType(BusinessConstants.GOODS_TYPE_HARDWARE);
                            lvRukumingxiNew.setIsdo(BusinessConstants.GOODS_DO_NO);
                            try {
                                boolean arriveTimeMatch = Pattern.matches("[0-9]+[.]{0,1}[0-9]*[dD]{0,1}", lvRukumingxi.getRukudateStr());
                                if (!arriveTimeMatch) {
                                    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
                                    lvRukumingxiNew.setRukudate(df.parse(lvRukumingxi.getRukudateStr()));
                                } else {
                                    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
                                    String date = df.format(DateUtil.getJavaDate(Double.parseDouble(lvRukumingxi.getRukudateStr())));
                                    lvRukumingxiNew.setRukudate(DateUtils.parseDate(date, "yyyy-MM-dd"));
                                }
                            } catch (Exception e) {
                                errortext += "行号为 " + lvRukumingxi.getIdx() + "行的数据，入库时间时间格式错误，请修改后再上传！";
//                        map.put("errorText", errortext);
//                        return map;
                                break;
                            }
                            try {
                                if(StringUtils.isNotBlank(lvRukumingxi.getDaohuoCountStr())){
                                    lvRukumingxiNew.setDaohuoCount(new BigDecimal(lvRukumingxi.getDaohuoCountStr()));
                                }
                            } catch (Exception e) {
                                errortext += "行号为 " + lvRukumingxi.getIdx() + "行的数据，数量格式错误，请修改后再上传！";
                                break;
                            }
                            try {
                                if(StringUtils.isNotBlank(lvRukumingxi.getGoodsBuyPriceStr())){
                                    lvRukumingxiNew.setGoodsBuyPrice(new BigDecimal(lvRukumingxi.getGoodsBuyPriceStr()));
                                }
                            } catch (Exception e) {
                                errortext += "行号为 " + lvRukumingxi.getIdx() + "行的数据，单价格式错误，请修改后再上传！";
                                break;
                            }
                            try {
                                if(StringUtils.isNotBlank(lvRukumingxi.getGoodsTotalPriceStr())){
                                    lvRukumingxiNew.setGoodsTotalPrice(new BigDecimal(lvRukumingxi.getGoodsTotalPriceStr()));
                                }
                            } catch (Exception e) {
                                errortext += "行号为 " + lvRukumingxi.getIdx() + "行的数据，金额格式错误，请修改后再上传！";
                                break;
                            }
                            listMingxi.add(lvRukumingxiNew);
                        }else{
                            goodsOrderIn=goodsOrderInMapper.selectByOtherCode(lvRukumingxi.getCode()+",");
                            if(goodsOrderIn!=null){
                                BeanUtils.copyProperties(lvRukumingxi, lvRukumingxiNew, "id");
                                lvRukumingxiNew.setGoodsId(goodsOrderIn.getId());
                                lvRukumingxiNew.setStatus(BusinessConstants.STORAGE_STATE_YES);
                                lvRukumingxiNew.setIsZhijian(BusinessConstants.INSPECT_STATE_YES);
                                lvRukumingxiNew.setType(BusinessConstants.TYPR_CHUKU);
                                lvRukumingxiNew.setGoodsType(BusinessConstants.GOODS_TYPE_HARDWARE);
                                lvRukumingxiNew.setIsdo(BusinessConstants.GOODS_DO_NO);
                                try {
                                    boolean arriveTimeMatch = Pattern.matches("[0-9]+[.]{0,1}[0-9]*[dD]{0,1}", lvRukumingxi.getRukudateStr());
                                    if (!arriveTimeMatch) {
                                        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
                                        lvRukumingxiNew.setRukudate(df.parse(lvRukumingxi.getRukudateStr()));
                                    } else {
                                        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
                                        String date = df.format(DateUtil.getJavaDate(Double.parseDouble(lvRukumingxi.getRukudateStr())));
                                        lvRukumingxiNew.setRukudate(DateUtils.parseDate(date, "yyyy-MM-dd"));
                                    }
                                } catch (Exception e) {
                                    errortext += "行号为 " + lvRukumingxi.getIdx() + "行的数据，入库时间时间格式错误，请修改后再上传！";
                                    break;
                                }
                                try {
                                    if(StringUtils.isNotBlank(lvRukumingxi.getDaohuoCountStr())){
                                        lvRukumingxiNew.setDaohuoCount(new BigDecimal(lvRukumingxi.getDaohuoCountStr()));
                                    }
                                } catch (Exception e) {
                                    errortext += "行号为 " + lvRukumingxi.getIdx() + "行的数据，数量格式错误，请修改后再上传！";
                                    break;
                                }
                                try {
                                    if(StringUtils.isNotBlank(lvRukumingxi.getGoodsBuyPriceStr())){
                                        lvRukumingxiNew.setGoodsBuyPrice(new BigDecimal(lvRukumingxi.getGoodsBuyPriceStr()));
                                    }
                                } catch (Exception e) {
                                    errortext += "行号为 " + lvRukumingxi.getIdx() + "行的数据，单价格式错误，请修改后再上传！";
                                    break;
                                }
                                try {
                                    if(StringUtils.isNotBlank(lvRukumingxi.getGoodsTotalPriceStr())){
                                        lvRukumingxiNew.setGoodsTotalPrice(new BigDecimal(lvRukumingxi.getGoodsTotalPriceStr()));
                                    }
                                } catch (Exception e) {
                                    errortext += "行号为 " + lvRukumingxi.getIdx() + "行的数据，金额格式错误，请修改后再上传！";
                                    break;
                                }
                                listMingxi.add(lvRukumingxiNew);
                            }else{
                                goodsOrderIn=goodsOrderInMapper.selectByOtherCode(lvRukumingxi.getCode()+",");
                                if(goodsOrderIn!=null){
                                    BeanUtils.copyProperties(lvRukumingxi, lvRukumingxiNew, "id");
                                    lvRukumingxiNew.setGoodsId(goodsOrderIn.getId());
                                    lvRukumingxiNew.setStatus(BusinessConstants.STORAGE_STATE_YES);
                                    lvRukumingxiNew.setIsZhijian(BusinessConstants.INSPECT_STATE_YES);
                                    lvRukumingxiNew.setType(BusinessConstants.TYPR_CHUKU);
                                    lvRukumingxiNew.setGoodsType(BusinessConstants.GOODS_TYPE_HARDWARE);
                                    lvRukumingxiNew.setIsdo(BusinessConstants.GOODS_DO_NO);
                                    try {
                                        boolean arriveTimeMatch = Pattern.matches("[0-9]+[.]{0,1}[0-9]*[dD]{0,1}", lvRukumingxi.getRukudateStr());
                                        if (!arriveTimeMatch) {
                                            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
                                            lvRukumingxiNew.setRukudate(df.parse(lvRukumingxi.getRukudateStr()));
                                        } else {
                                            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
                                            String date = df.format(DateUtil.getJavaDate(Double.parseDouble(lvRukumingxi.getRukudateStr())));
                                            lvRukumingxiNew.setRukudate(DateUtils.parseDate(date, "yyyy-MM-dd"));
                                        }
                                    } catch (Exception e) {
                                        errortext += "行号为 " + lvRukumingxi.getIdx() + "行的数据，入库时间时间格式错误，请修改后再上传！";
                                        break;
                                    }
                                    try {
                                        if(StringUtils.isNotBlank(lvRukumingxi.getDaohuoCountStr())){
                                            lvRukumingxiNew.setDaohuoCount(new BigDecimal(lvRukumingxi.getDaohuoCountStr()));
                                        }
                                    } catch (Exception e) {
                                        errortext += "行号为 " + lvRukumingxi.getIdx() + "行的数据，数量格式错误，请修改后再上传！";
                                        break;
                                    }
                                    try {
                                        if(StringUtils.isNotBlank(lvRukumingxi.getGoodsBuyPriceStr())){
                                            lvRukumingxiNew.setGoodsBuyPrice(new BigDecimal(lvRukumingxi.getGoodsBuyPriceStr()));
                                        }
                                    } catch (Exception e) {
                                        errortext += "行号为 " + lvRukumingxi.getIdx() + "行的数据，单价格式错误，请修改后再上传！";
                                        break;
                                    }
                                    try {
                                        if(StringUtils.isNotBlank(lvRukumingxi.getGoodsTotalPriceStr())){
                                            lvRukumingxiNew.setGoodsTotalPrice(new BigDecimal(lvRukumingxi.getGoodsTotalPriceStr()));
                                        }
                                    } catch (Exception e) {
                                        errortext += "行号为 " + lvRukumingxi.getIdx() + "行的数据，金额格式错误，请修改后再上传！";
                                        break;
                                    }
                                    listMingxi.add(lvRukumingxiNew);
                                }
                                else{
                                    codes = codes+lvRukumingxi.getCode()+",";
                                   /* errortext += "序号为" + lvRukumingxi.getIdx() + "行数据错误，存货编号："+lvRukumingxi.getCode()+"在成品库中不存在，请在产品库中新建新产品后导入！";
                                    map.put("errorText", errortext);
                                    map.put("num", i);
                                    map.put("numupdate", j);
                                    return map;*/
                                }
                            }
                        }
                    }
                    // i++;
                    if (StringUtils.isNotBlank(errortext)) {
                        map.put("errorText", errortext);
                        map.put("num", i);
                        map.put("numupdate", j);
                        return map;
                    }
                }
            }
            if (StringUtils.isBlank(errortext)) {
                for (LvRukumingxi info : listMingxi) {
                    i += this.insert(info);
                    GoodsOrderIn goodsOrderInUodate=goodsOrderInMapper.selectById(info.getGoodsId());
                    goodsOrderInUodate.setCount(goodsOrderInUodate.getCount().subtract(info.getDaohuoCount()));
                    goodsOrderInUodate.setRemainderCount(goodsOrderInUodate.getRemainderCount().subtract(info.getDaohuoCount()));
                    if(info.getGoodsBuyPrice()!=null){
                        goodsOrderInUodate.setCostPrice(info.getGoodsBuyPrice());
                        if(info.getGoodsBuyPrice()!=null && goodsOrderInUodate.getRemainderCount()!=null) {
                            goodsOrderInUodate.setCost((info.getGoodsBuyPrice().multiply(goodsOrderInUodate.getRemainderCount())));
                        }
                    }
                    goodsOrderInMapper.update(goodsOrderInUodate);

                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        map.put("errorText", errortext);
        map.put("num", i);
        map.put("codes", codes);
        map.put("numupdate", j);
        return map;
    }
    @Override
    public Map<String, Object> uploadOut(LvRukumingxi entity, MultipartFile file) {
        String tmplName = "uploadOutMingxi.xml";
        String errortext = "";
        String codes = "";
        Map<String, Object> map = new HashMap<>();
        int i = 0;
        int j = 0;
        List<LvRukumingxi> list = new ArrayList<>();
        try {
            InputStream in = new FileInputStream(basePath + File.separator + templatePath + File.separator + templateImport + File.separator + tmplName);
            XLSReader mainReader = ReaderBuilder.buildFromXML(in);
            Map beans = new HashMap();
            List<LvRukumingxi> listMingxi = new ArrayList<>();
            beans.put("list", list);
            XLSReadStatus readStatus = mainReader.read(file.getInputStream(), beans);
            if (readStatus.isStatusOK()) {
                for (LvRukumingxi lvRukumingxi : list) {
                    LvRukumingxi lvRukumingxiNew=new LvRukumingxi();
                    if (StringUtils.isNotBlank(lvRukumingxi.getCode())) {
                        GoodsOrderIn goodsOrderIn=goodsOrderInMapper.selectByCode(lvRukumingxi.getCode());
                        if(goodsOrderIn!=null){
                            //lvRukumingxi1.setCode(lvRukumingxi.getCode());
                            BeanUtils.copyProperties(lvRukumingxi, lvRukumingxiNew, "id");
                            lvRukumingxiNew.setGoodsId(goodsOrderIn.getId());
                            lvRukumingxiNew.setStatus(BusinessConstants.STORAGE_STATE_YES);
                            lvRukumingxiNew.setIsZhijian(BusinessConstants.INSPECT_STATE_YES);
                            lvRukumingxiNew.setType(BusinessConstants.TYPR_CHUKU);
                            lvRukumingxiNew.setGoodsType(BusinessConstants.GOODS_TYPE_HARDWARE);
                            lvRukumingxiNew.setIsdo(BusinessConstants.GOODS_DO_NO);
                            try {
                                boolean arriveTimeMatch = Pattern.matches("[0-9]+[.]{0,1}[0-9]*[dD]{0,1}", lvRukumingxi.getRukudateStr());
                                if (!arriveTimeMatch) {
                                    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
                                    lvRukumingxiNew.setRukudate(df.parse(lvRukumingxi.getRukudateStr()));
                                } else {
                                    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
                                    String date = df.format(DateUtil.getJavaDate(Double.parseDouble(lvRukumingxi.getRukudateStr())));
                                    lvRukumingxiNew.setRukudate(DateUtils.parseDate(date, "yyyy-MM-dd"));
                                }
                            } catch (Exception e) {
                                errortext += "行号为 " + lvRukumingxi.getIdx() + "行的数据，入库时间时间格式错误，请修改后再上传！";
//                        map.put("errorText", errortext);
//                        return map;
                                break;
                            }
                            try {
                                if(StringUtils.isNotBlank(lvRukumingxi.getDaohuoCountStr())){
                                    lvRukumingxiNew.setDaohuoCount(new BigDecimal(lvRukumingxi.getDaohuoCountStr()));
                                }
                            } catch (Exception e) {
                                errortext += "行号为 " + lvRukumingxi.getIdx() + "行的数据，数量格式错误，请修改后再上传！";
                                break;
                            }
                            try {
                                if(StringUtils.isNotBlank(lvRukumingxi.getGoodsBuyPriceStr())){
                                    lvRukumingxiNew.setGoodsBuyPrice(new BigDecimal(lvRukumingxi.getGoodsBuyPriceStr()));
                                }
                            } catch (Exception e) {
                                errortext += "行号为 " + lvRukumingxi.getIdx() + "行的数据，单价格式错误，请修改后再上传！";
                                break;
                            }
                            try {
                                if(StringUtils.isNotBlank(lvRukumingxi.getGoodsTotalPriceStr())){
                                    lvRukumingxiNew.setGoodsTotalPrice(new BigDecimal(lvRukumingxi.getGoodsTotalPriceStr()));
                                }
                            } catch (Exception e) {
                                errortext += "行号为 " + lvRukumingxi.getIdx() + "行的数据，金额格式错误，请修改后再上传！";
                                break;
                            }
                            listMingxi.add(lvRukumingxiNew);
                        }else{
                            goodsOrderIn=goodsOrderInMapper.selectByOtherCode(lvRukumingxi.getCode()+",");
                            if(goodsOrderIn!=null){
                                BeanUtils.copyProperties(lvRukumingxi, lvRukumingxiNew, "id");
                                lvRukumingxiNew.setGoodsId(goodsOrderIn.getId());
                                lvRukumingxiNew.setStatus(BusinessConstants.STORAGE_STATE_YES);
                                lvRukumingxiNew.setIsZhijian(BusinessConstants.INSPECT_STATE_YES);
                                lvRukumingxiNew.setType(BusinessConstants.TYPR_CHUKU);
                                lvRukumingxiNew.setGoodsType(BusinessConstants.GOODS_TYPE_HARDWARE);
                                lvRukumingxiNew.setIsdo(BusinessConstants.GOODS_DO_NO);
                                try {
                                    boolean arriveTimeMatch = Pattern.matches("[0-9]+[.]{0,1}[0-9]*[dD]{0,1}", lvRukumingxi.getRukudateStr());
                                    if (!arriveTimeMatch) {
                                        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
                                        lvRukumingxiNew.setRukudate(df.parse(lvRukumingxi.getRukudateStr()));
                                    } else {
                                        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
                                        String date = df.format(DateUtil.getJavaDate(Double.parseDouble(lvRukumingxi.getRukudateStr())));
                                        lvRukumingxiNew.setRukudate(DateUtils.parseDate(date, "yyyy-MM-dd"));
                                    }
                                } catch (Exception e) {
                                    errortext += "行号为 " + lvRukumingxi.getIdx() + "行的数据，入库时间时间格式错误，请修改后再上传！";
//                        map.put("errorText", errortext);
//                        return map;
                                    break;
                                }
                                try {
                                    if(StringUtils.isNotBlank(lvRukumingxi.getDaohuoCountStr())){
                                        lvRukumingxiNew.setDaohuoCount(new BigDecimal(lvRukumingxi.getDaohuoCountStr()));
                                    }
                                } catch (Exception e) {
                                    errortext += "行号为 " + lvRukumingxi.getIdx() + "行的数据，数量格式错误，请修改后再上传！";
                                    break;
                                }
                                try {
                                    if(StringUtils.isNotBlank(lvRukumingxi.getGoodsBuyPriceStr())){
                                        lvRukumingxiNew.setGoodsBuyPrice(new BigDecimal(lvRukumingxi.getGoodsBuyPriceStr()));
                                    }
                                } catch (Exception e) {
                                    errortext += "行号为 " + lvRukumingxi.getIdx() + "行的数据，单价格式错误，请修改后再上传！";
                                    break;
                                }
                                try {
                                    if(StringUtils.isNotBlank(lvRukumingxi.getGoodsTotalPriceStr())){
                                        lvRukumingxiNew.setGoodsTotalPrice(new BigDecimal(lvRukumingxi.getGoodsTotalPriceStr()));
                                    }
                                } catch (Exception e) {
                                    errortext += "行号为 " + lvRukumingxi.getIdx() + "行的数据，金额格式错误，请修改后再上传！";
                                    break;
                                }
                                listMingxi.add(lvRukumingxiNew);
                            }
                            else {
                                codes = codes+lvRukumingxi.getCode()+",";
                               /* errortext += "序号为" + lvRukumingxi.getIdx() + "行数据错误，存货编号："+lvRukumingxi.getCode()+"在成品库中不存在，请在产品库中新建新产品后导入！";
                                map.put("errorText", errortext);
                                map.put("num", i);
                                map.put("numupdate", j);
                                return map;*/
                            }
                        }
                    }
                    // i++;
                    if (StringUtils.isNotBlank(errortext)) {
                        map.put("errorText", errortext);
                        map.put("num", i);
                        map.put("numupdate", j);
                        return map;
                    }
                }
            }
            if (StringUtils.isBlank(errortext)) {
                for (LvRukumingxi info : listMingxi) {
                    i += this.insert(info);
                    GoodsOrderIn goodsOrderInUodate=goodsOrderInMapper.selectById(info.getGoodsId());
                    goodsOrderInUodate.setCount(goodsOrderInUodate.getCount().subtract(info.getDaohuoCount()));
                    goodsOrderInUodate.setRemainderCount(goodsOrderInUodate.getRemainderCount().subtract(info.getDaohuoCount()));
                    if(info.getGoodsBuyPrice()!=null) {
                        goodsOrderInUodate.setCostPrice(info.getGoodsBuyPrice());
                        goodsOrderInUodate.setCost((info.getGoodsBuyPrice().multiply(goodsOrderInUodate.getRemainderCount())));
                    }
                    goodsOrderInMapper.update(goodsOrderInUodate);

                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        map.put("errorText", errortext);
        map.put("codes", codes);
        map.put("num", i);
        map.put("numupdate", j);
        return map;
    }

    @Override
    public String selectMaxCode(String today) {
        return lvRukumingxiMapper.selectMaxCode(today);
    }


    /**
     * 查询采购的未完全开票的入库记录
     * @param entity 入库记录
     * @return 入库记录
     */
    public List<LvRukumingxi> buyNotInvoiceList(LvRukumingxi entity) {
        return lvRukumingxiMapper.buyNotInvoiceList(entity);
    }

    @Override
    public void exportDataIn(HttpServletRequest request, HttpServletResponse response, LvRukumingxi entity) {
        List<LvRukumingxi> list = lvRukumingxiMapper.selectList(entity);
        for (LvRukumingxi lvRukumingxi : list) {
            Map<String, Object> params = lvRukumingxi.getParams();
            params.put("rukudate", DateUtils.formatDate(lvRukumingxi.getRukudate(), "yyyy-MM-dd"));
            params.put("isdo", DictUtils.getDictLabel("isdo", lvRukumingxi.getIsdo()));
            params.put("status", DictUtils.getDictLabel("storage_state", lvRukumingxi.getStatus()));
            lvRukumingxi.setParams(params);
        }
        String fileName = "入库产品明细.xlsx";
        String templateName = "入库产品明细.xlsx";
        Map<String, Object> params = new HashMap<>();
        params.put("list", list);
        exportService.baseXlsxExport(fileName, params, templateName, request, response);
    }

    @Override
    public void exportDataOut(HttpServletRequest request, HttpServletResponse response, LvRukumingxi entity) {
        List<LvRukumingxi> list = lvRukumingxiMapper.selectList(entity);
        for (LvRukumingxi lvRukumingxi : list) {
            Map<String, Object> params = lvRukumingxi.getParams();
            params.put("rukudate", DateUtils.formatDate(lvRukumingxi.getRukudate(), "yyyy-MM-dd"));
            params.put("isdo", DictUtils.getDictLabel("isdo", lvRukumingxi.getIsdo()));
            params.put("status", DictUtils.getDictLabel("storage_state", lvRukumingxi.getStatus()));
            lvRukumingxi.setParams(params);
        }
        String fileName = "出库产品明细.xlsx";
        String templateName = "出库产品明细.xlsx";
        Map<String, Object> params = new HashMap<>();
        params.put("list", list);
        exportService.baseXlsxExport(fileName, params, templateName, request, response);
    }

    @Override
    public int addRukumingxiList(LvRukumingxi entity) {
        List<LvRukumingxi> rukumingxis = entity.getRukumingxiList();
        for (LvRukumingxi rukumingxi : rukumingxis) {
            LvRukumingxi rukumingxi1=lvRukumingxiMapper.selectById(rukumingxi.getId());
            LvRukumingxi rukumingxi2 = new LvRukumingxi();
            BeanUtils.copyProperties(rukumingxi1, rukumingxi2, "id");
            rukumingxi2.setMingxiId(rukumingxi.getId());
            rukumingxi2.setCanCount(BigDecimal.ZERO);
            rukumingxi2.setDaohuoCount(rukumingxi.getUseCount());
            rukumingxi2.setInvoiceStatus("");
            rukumingxi2.setInvoiceAmount(BigDecimal.ZERO);
            rukumingxi2.setInvoiceRemain(BigDecimal.ZERO);
            rukumingxi2.setSource("2");
            rukumingxi2.setStatus("0");
            rukumingxi2.setType("1");
            insert(rukumingxi2);

            //rukumingxi1.setCanCount(goodsSaleMaterial1.getLingquCount().add(goodsSaleMaterial.getShengyuCount()));
            if(rukumingxi1.getCanCount()!=null && rukumingxi.getUseCount()!=null){
                rukumingxi1.setCanCount(rukumingxi1.getCanCount().subtract(rukumingxi.getUseCount()));
            }
            update(rukumingxi1);
            GoodsOrderIn goodsOrderIn=goodsOrderInMapper.selectById(rukumingxi1.getGoodsId());
            if(goodsOrderIn.getRemainderCount()!=null && rukumingxi.getUseCount()!=null) {
                goodsOrderIn.setRemainderCount(goodsOrderIn.getRemainderCount().subtract(rukumingxi.getUseCount()));
            }
            goodsOrderInMapper.update(goodsOrderIn);

        }
        return 1 ;
    }

    /**
     * 删除后只退可用数量
     * @param ids
     * @return
     */
    @Override
    public int deleteRukumingxi(String[] ids) {
        for(String id:ids){
            LvRukumingxi rukumingxi=lvRukumingxiMapper.selectById(id);
            if(rukumingxi!=null){
                LvRukumingxi rukumingxi1=lvRukumingxiMapper.selectById(rukumingxi.getMingxiId());
                if(rukumingxi1!=null){
                    rukumingxi1.setCanCount(rukumingxi1.getCanCount().add(rukumingxi.getDaohuoCount()));
                    update(rukumingxi1);
                    GoodsOrderIn goodsOrderIn=goodsOrderInMapper.selectById(rukumingxi.getGoodsId());
                    if(goodsOrderIn.getRemainderCount()!=null && rukumingxi.getDaohuoCount()!=null) {
                        goodsOrderIn.setRemainderCount(goodsOrderIn.getRemainderCount().add(rukumingxi.getDaohuoCount()));
                    }
                    goodsOrderInMapper.update(goodsOrderIn);
                    lvRukumingxiMapper.deleteById(id);
                }
            }
        }
        return 1;
    }

    @Override
    public int addMainOtherChuku(LvRuku entity) {
        if(StringUtils.isBlank(entity.getId())){
            entity.setId(IdUtils.fastSimpleUUID());
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String today = sdf.format(new Date());
        String codeMaxString = lvRukuService.selectMaxCode(today);
        if(StringUtils.isNotBlank(codeMaxString)){
            // 最大编号+1
            codeMaxString = Long.parseLong(codeMaxString)+1+"";
        }else{
            codeMaxString = today+"0001";
        }
        entity.setNos(codeMaxString);
        entity.setCreateBy(SecurityUtils.getUsername());
        entity.setCreateTime(DateUtils.getNowDate());
        GoodsSupplier goodsSupplier=iGoodsSupplierService.selectById(entity.getSupplierId());
        if(goodsSupplier!=null){
            entity.setSupplierName(goodsSupplier.getName());
        }
        lvRukuService.insert(entity);
        List<LvRukumingxi> goodsSaleMaterialChildList=entity.getDetailList();
        for(LvRukumingxi lvRukumingxi:goodsSaleMaterialChildList){
            lvRukumingxi.setParentId(entity.getId());
            lvRukumingxi.setRukuno(codeMaxString);
            lvRukumingxi.setStatus(BusinessConstants.STORAGE_STATE_YES);
            lvRukumingxi.setSource(entity.getSource());
            lvRukumingxi.setMingxiId(lvRukumingxi.getId());
            update(lvRukumingxi);
            GoodsOrderIn goodsOrderIn=goodsOrderInMapper.selectById(lvRukumingxi.getGoodsId());
            if(goodsOrderIn.getCount()!=null && lvRukumingxi.getDaohuoCount()!=null) {
                goodsOrderIn.setCount(goodsOrderIn.getCount().subtract(lvRukumingxi.getDaohuoCount()));
            }
            goodsOrderInMapper.update(goodsOrderIn);
        }
        return 1 ;
    }
}
